package com.hua.yuan.live;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.hua.yuan.base.BaseAppActivity;
import com.hua.yuan.base.Constents;
import com.hua.yuan.dialog.ConfirmCancelDialog;
import com.hua.yuan.live.adapter.LiveReservationAdapter;
import com.hua.yuan.live.listener.OnBuyClickListener;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveReservationLoader;
import com.hua.yuan.live.repertory.cloud_data.loader.LiveGetReservationLoader;
import com.hua.yuan.live.repertory.cloud_data.res.ResercationBean;

import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;

/**
 * 时间：2018/8/22
 * 作者：蓝天
 * 描述：预约视频
 */
public class LiveVideoActivity extends BaseAppActivity implements View.OnClickListener,
        LoaderManager.LoaderCallbacks<List<ResercationBean>>,
        OnBuyClickListener, ConfirmCancelDialog.OnConfirmListener,
        LiveReservationLoader.onReservationCallback
{
    private ImageView backImage;
    private TextView titleView;
    private TextView tv_anchor_name;
    private ImageView iv_anchor_cover;
    private ListView mVideoListView;
    private LiveReservationAdapter mAdapter;
    private LiveGetReservationLoader mReservationLoader;
    public final static int TASK_ID = 1004;
    private ConfirmCancelDialog mConfigDialog;
    private int mPosition;
    private int money;
    private String mToken;
    private int mAnchorId;



    @Override
    protected int initLayoutId() {
        return R.layout.activity_live_video;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void initView() {
        mAdapter = new LiveReservationAdapter(this, this);
        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);
        tv_anchor_name = findViewById(R.id.tv_anchor_name);
        iv_anchor_cover = findViewById(R.id.iv_anchor_cover);
        mVideoListView = findViewById(R.id.lv_videolist);
        mVideoListView.setAdapter(mAdapter);
        mToken = PreferenceUtils.getString(Constents.TOKEN);
        mAnchorId = getIntent().getIntExtra(LiveModule.INTENT_ANCHOR_ID, 0);

        mReservationLoader = (LiveGetReservationLoader) getSupportLoaderManager().initLoader(TASK_ID, null, this);
        mReservationLoader.requestData(mToken, mAnchorId);
        mReservationLoader.forceLoad();
        mConfigDialog = ConfirmCancelDialog.create(getSupportFragmentManager());
        mConfigDialog.setOnConfirmListener(this);
        money = getIntent().getIntExtra(LiveModule.INTENT_USER_MONEY, 0);
    }

    @Override
    protected void initNet() {

//                backImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });
//        titleView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ConfirmCancelDialog.create(getSupportFragmentManager()).show();
//            }
//        });
////        tv_anchor_name.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View view) {
//////                Intent intent = new Intent(LiveVideoActivity.this, LiveVideoP2PActivity.class);
//////                startActivity(intent);
////                Intent intent = new Intent(LiveVideoActivity.this, RTCDoubleRoomActivity.class);
////                startActivity(intent);
////            }
////        });
//
//        List<String> list = new ArrayList<>();
//        list.add("");
//        list.add("");
//        list.add("");
//        list.add("");
//        list.add("");
//        lv_videolist.setAdapter(new ListAdapter(list));

    }


    @Override
    public void onClick(View v) {

    }

    @NonNull
    @Override
    public Loader<List<ResercationBean>> onCreateLoader(int id, @Nullable Bundle args) {
        return new LiveGetReservationLoader(this);
    }

    @Override
    public void onLoadFinished(@NonNull Loader<List<ResercationBean>> loader, List<ResercationBean> data) {

        if (data == null) {
            return;
        }
        mAdapter.setData(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(@NonNull Loader<List<ResercationBean>> loader) {

    }

    @Override
    public void click(int position) {

        mPosition = position;
        mConfigDialog.show();

    }

    @Override
    public void onConfirm(View v) {

        if (mPosition > mAdapter.getData().size()) {
            return;
        }
        ResercationBean bean = (ResercationBean) mAdapter.getData().get(mPosition);
        if (bean == null) {
            return;
        }
        //如果未预约
        if (bean.getHasAppointment() == 0) {
            //可购买
            //判断当前是否是可预约的
            if (money < bean.getMoney()) {
                Intent intent = new Intent();
                intent.setClassName(this, "com.hua.yuan.ui.PlayActivity");
                if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
                    startActivity(intent);
                    return;
                }
            }else {
                LiveReservationLoader.reservation(mToken, mAnchorId, bean.getStartTime(), bean.getEndTime(), String.valueOf(money), this);
            }
        } else {
            //如果已经预约，且时间符合，则进入
            Intent intent = new Intent();
            intent.setClassName(this, "com.hua.yuan.rtcroom.ui.double_room.RTCDoubleRoomActivity");
            if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
                startActivity(intent);
            }
            return;
        }
    }

    @Override
    public void resevation(Integer isSuccess) {

        if(isSuccess==0){
            //连接成功
            Toast.makeText(getApplicationContext(),"预约成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(),"预约失败",Toast.LENGTH_SHORT).show();
        }
    }
}
